// components/multi_list_swiper/multi_list_swiper.js

/* 主页面
 */
const app = getApp()
Component({
    /**
     * 属性
     */
    properties: {
        /*
         * 当前页面位置
         */
        current: {
            type: Number,
            value: 1 //默认在表白墙里
        },
        useHole: {
            type: Boolean,
            value: true //默认启用树洞
        },
        useWall: {
            type: Boolean,
            value: true //默认启用表白墙
        }
    },

    /**
     * 数据
     */
    data: {
        loadingData: false, //是否在加载数据

        wallData: [],
        wallRepetition: [],

        holeDataLeft: [],
        holeDataRight: [],
        holeRepetition: [],

        //不精确方案
        holeHeightLeft: 0, //尽量让两边洞高度差不多,这里统计字数来做
        holeHeightRight: 0,
    },

    /**
     * 方法
     */
    methods: {
        getWall: function (msg) {
            let that = this
            let listData = this.data.wallData
            let repetition = this.data.wallRepetition
            var wallList = [];
            var get_wall_data = {
                pageIndex: 1,
                pageSize: 5,
                textIds: repetition
            }
            app.wxapi('POST', "/wall/content/get-hot-walls", get_wall_data, 'application/x-www-form-urlencoded', res => {
                //console.log(res);
                if (res.success) {
                    for (var i = 0; i < res.data.length; i++) {
                        var wallData = res.data[i];
                        wallList.push(wallData);
                        repetition.push(res.data[i].wallId)
                    }
                    /*
                     * 导入数据
                     */
                    listData = listData.concat(wallList)
                    this.setData({
                        wallData: listData,
                        wallRepetition: repetition
                    })
                } else {
                    wx.showToast({
                        title: msg,
                    })
                    console.log("没有回应");
                }
            }, (err) => {
                //调用服务端登录接口失败
                console.log(err.errMsg)
            })
        },
        getHole: function (msg) {
            let that = this
            let listDataLeft = this.data.holeDataLeft
            let listDataRight = this.data.holeDataRight
            let repetition = this.data.holeRepetition
            var holeList = [];
            var get_hole_data = {
                pageIndex: 0,
                pageSize: 10,
                excludeIds: repetition
            }
            app.wxapi('POST', "/hole/get-hot-holes", get_hole_data, 'application/x-www-form-urlencoded', res => {
                const query = wx.createSelectorQuery().in(this) // 创建节点查询器 query
                console.log(res)
                if (res.success) {
                    for (var i = 0; i < res.data.length; i++) {
                        var holeData = res.data[i];
                        holeData.name = res.data[i].holeUserName;
                        holeData.dianzanNum = res.data[i].holeThumb;
                        //console.log(typeof holeData.text)
                        //console.log(holeData);
                        // 精确方案但是有延时，不行
                        /*
                        query.select('#holeLeft').boundingClientRect() 
                        query.select('#holeRight').boundingClientRect()
                        query.exec((res) => {
                            console.log(that.data.holeDataLeft, "list")
                            if(res[0].height <= res[1].height) {
                                var index = "holeDataLeft[" + that.data.holeDataLeft.length + "]";
                                that.setData({
                                    [index]: holeData
                                })
                            } else {
                                var index = "holeDataRight[" + that.data.holeDataRight.length + "]";
                                that.setData({
                                    [index]: holeData
                                })
                            }
                        })
                        */
                        // TODO 这个函数待改进，可以大概计算一项的长度
                        var maybeHeight = that.calcHeight(holeData.holeContent);
                        console.log(maybeHeight, "maybeHeight");
                        console.log(that.data.holeHeightLeft, "left");
                        console.log(that.data.holeHeightRight, "right");
                        if(that.data.holeHeightLeft <= that.data.holeHeightRight) {
                            listDataLeft.push(holeData);
                            that.setData({
                                holeHeightLeft: that.data.holeHeightLeft + maybeHeight
                            })
                        } else {
                            listDataRight.push(holeData);
                            that.setData({
                                holeHeightRight: that.data.holeHeightRight + maybeHeight
                            })
                        }
                        repetition.push(res.data[i].holeID);
                    }
                    //console.log(holeList);
                    /*
                     * 导入数据
                     */
                    that.setData({
                        holeDataLeft: listDataLeft,
                        holeDataRight: listDataRight,
                        holeRepetition: repetition
                    })
                } else {
                    wx.showToast({
                        title: msg,
                    })
                    console.log("没有回应");
                }
            }, (err) => {
                //调用服务端登录接口失败
                console.log(err.errMsg)
            })
        },
        calcHeight: function (text) {
            console.log(text, "text length");
            var base = 10;
            var oneLine = 10;
            var textLength = ("" + text).length;
            return base + 1 * Math.floor(textLength / oneLine);
        },
        scrollToLower: function () {
            console.log("页面触底");
            //console.log(this.properties.current);
            if (this.data.loadingData) {
                return;
            }
            this.setData({
                loadingData: true
            });
            // 这里由于某种耦合关系，current 的值在follow直接设置会有问题。所以得这样判断
            if (this.properties.current == 0 && this.properties.useHole) {
                this.getHole("到底了");
            } else {
                this.getWall("到底了");
            }
            this.setData({
                loadingData: false
            })
        },
        wallSwitchLike: function (param) {
            //console.log(param.detail)
            let wallData = this.data.wallData
            wallData[param.detail.entryIndex].wallliked = param.detail.liked
            this.setData({
                wallData
            })
        }
    },

    /*
     * 生命周期
     */
    lifetimes: {
        /*
         * 组件进入节点树时
         */
        ready: function () {
            this.getWall();
            this.getHole();
        }
    },

})